<!--
 * Copyright © 2022 XXXXXXXXX
 * @Author: zhangxiang
 * @Date: 2022-08-04 18:01:36
 * @LastEditors: zhangxiang
 * @LastEditTime: 2023-09-21 15:02:45
 * @Description:
 *
-->

<template>
  <div class="pods-mgmt-ctn">
    <el-row type="flex" style="margin-bottom: 20px">
      <el-input
        v-model="searchKey"
        style="width: 200px; margin-right: 20px"
        size="small"
        class="input"
        placeholder="请输入关键字进行过滤"
        clearable
        @clear="clearClick"
        @keyup.enter.native="queryClick"
      />
      <el-button
        size="small"
        type="primary"
        @click="queryClick"
      >查询</el-button>
      <el-tag
        v-if="svcName"
        style="margin-left: 20px"
        closable
        @close="svcCloseClick"
      >
        {{ svcName }}
      </el-tag>
      <el-tag
        v-if="workLoadName"
        style="margin-left: 20px"
        closable
        @close="workLoadCloseClick"
      >
        {{ workLoadName }}
      </el-tag>
    </el-row>
    <el-table
      key="pods"
      v-loading="loading"
      class="pods-table table-fixed"
      :data="list"
      header-cell-class-name="listHeaderCell"
      :header-cell-style="{ background: '#F3F3F3', color: '#696969' }"
      border
    >
      <el-table-column
        prop="cloudVendor"
        label="云商名称"
        min-width="110"
        show-overflow-tooltip=""
      >
        <template slot-scope="scope">
          {{ cloudVendorToChn(scope.row.cloudVendor) }}
        </template>
      </el-table-column>
      <el-table-column
        prop="k8sClusterName"
        label="集群名称"
        min-width="180"
        show-overflow-tooltip=""
      />
      <el-table-column
        prop="podName"
        label="容器组名称"
        min-width="250"
        show-overflow-tooltip=""
      />
      <el-table-column
        prop="namespace"
        label="命名空间"
        width="120"
        show-overflow-tooltip=""
      />
      <el-table-column
        prop="nodeName"
        label="所在节点"
        min-width="165"
        show-overflow-tooltip=""
      />
      <el-table-column prop="podIP" label="Pod IP" min-width="130" />
      <el-table-column prop="status" label="状态" width="120" />
      <el-table-column prop="containerNum" label="容器数" width="100" />
      <el-table-column prop="restartTimes" label="重启次数" width="100" />
      <el-table-column prop="age" label="Age" width="120" />
      <el-table-column prop="cpuUse" label="CPU使用量" width="120" />
      <el-table-column prop="memUse" label="内存使用量" width="120" />
      <el-table-column label="操作" width="150" fixed="right">
        <template slot-scope="scope">
          <el-button
            type="primary"
            size="mini"
            style="height: 22px; line-height: 22px; padding: 0 10px"
            @click="podContainersClick(scope.$index)"
          >容器列表</el-button>
          <el-popover
            :ref="`popover-delete-${scope.$index}`"
            placement="top"
            width="300"
            style="margin-left: 10px"
          >
            <p>是否确认删除该容器组？</p>
            <div style="text-align: right; margin: 0">
              <el-button
                size="mini"
                type="text"
                @click="
                  scope._self.$refs[`popover-delete-${scope.$index}`].doClose()
                "
              >取消</el-button>
              <el-button
                type="primary"
                size="mini"
                @click="deleteClick(scope)"
              >确定</el-button>
            </div>
            <el-button
              slot="reference"
              type="danger"
              style="height: 22px; line-height: 22px; padding: 0 10px"
              size="mini"
            >删除</el-button>
          </el-popover>
        </template>
      </el-table-column>
    </el-table>
    <el-row type="flex" justify="end" style="margin-top: 25px">
      <el-pagination
        background
        layout="total, sizes, prev, pager, next"
        :page-sizes="[10, 15, 20]"
        :page-size="pageSize"
        :current-page.sync="pageNum"
        :total="totalNum"
        @current-change="changePage"
        @size-change="sizeChange"
      />
    </el-row>

    <el-dialog
      class="container-list-dialog"
      title="容器列表"
      :visible="showContainerDialog"
      width="50%"
      top="15vh"
      @close="showContainerDialog = false"
    >
      <el-table
        key="container"
        v-loading="containerLoading"
        class="pods-container-table"
        :data="containerList"
        header-cell-class-name="listHeaderCell"
        :header-cell-style="{ background: '#F3F3F3', color: '#696969' }"
      >
        <el-table-column
          prop="containerName"
          label="容器名称"
          show-overflow-tooltip=""
          min-width="140"
        />
        <el-table-column prop="status" label="状态" width="100" />
        <el-table-column
          prop="containerImage"
          label="镜像"
          min-width="300"
          show-overflow-tooltip=""
        />
        <el-table-column prop="restartTimes" label="重启次数" width="100" />
      </el-table>
    </el-dialog>
  </div>
</template>

<script>
import commonTab from '../mixin/commonTab'
import {
  getK8sClusterPods,
  getK8sClusterPodContainers,
  delK8sClusterPod
} from '@/api/cnap/hybrid-cloud'

export default {
  name: 'PodsMgmt',
  mixins: [commonTab],
  data() {
    return {
      svcName: '', // 服务名
      workLoadName: '', // 负载名
      namespace: '', // namespace
      showContainerDialog: false, // 展示弹窗
      showIndex: 0, // 展示数据index
      containerLoading: false, // 容器加载中
      containerList: [] // 容器列表
    }
  },
  methods: {
    /**
     * @description: 展示容器列表
     * @return {*}
     */
    getClusterPodContainersInfo() {
      this.containerLoading = true
      // 请求
      getK8sClusterPodContainers({
        cloudVendor: this.list[this.showIndex].cloudVendor,
        k8sClusterName: this.list[this.showIndex].k8sClusterName,
        namespace: this.list[this.showIndex].namespace,
        podName: this.list[this.showIndex].podName
      })
        .then((res) => {
          this.containerList = Array.isArray(res.data) ? res.data : []
        })
        .finally(() => {
          this.containerLoading = false
        })
    },
    /**
     * @description: 容器列表点击
     * @param {*} index
     * @return {*}
     */
    podContainersClick(index) {
      this.showIndex = index
      this.showContainerDialog = true // 展示容器列表弹窗
      this.getClusterPodContainersInfo() // 请求数据
    },
    /**
     * @description: 关闭负载标签
     * @return {*}
     */
    workLoadCloseClick() {
      this.workLoadName = ''
      this.namespace = ''
      this.pageNum = 1
      this.getPodsInfo() // 请求数据
    },
    /**
     * @description: 关闭服务标签
     * @return {*}
     */
    svcCloseClick() {
      this.svcName = ''
      this.namespace = ''
      this.pageNum = 1
      this.getPodsInfo() // 请求数据
    },
    /**
     * @description: 删除
     * @param {*} scope
     * @return {*}
     */
    deleteClick(scope) {
      const row = scope.row
      // 请求
      delK8sClusterPod({
        ...this.clusterParams,
        namespace: row.namespace,
        podName: row.podName
      }).then((res) => {
        scope._self.$refs[`popover-delete-${scope.$index}`].doClose() // 取消popover
        this.$message.success('删除成功')
        if (this.list.length === 1) {
          // 当前页只有一条数据时 删除完刷新上一页数据
          this.pageNum = Math.max(this.pageNum - 1, 1)
        }
        this.getPodsInfo() // 请求数据
      })
    },
    /**
     * @description: 每页显示size变化
     * @param {*} size
     * @return {*}
     */
    sizeChange(size) {
      this.pageSize = size
      this.pageNum = 1
      this.getPodsInfo() // 请求数据
    },
    /**
     * @description: 当前页改变
     * @param {*} pageNum
     * @return {*}
     */
    changePage(pageNum) {
      this.pageNum = pageNum
      this.getPodsInfo() // 请求数据
    },
    /**
     * @description: 清除关键字
     * @return {*}
     */
    clearClick() {
      this.searchKey = ''
      this.pageNum = 1
      this.getPodsInfo() // 请求数据
    },
    /**
     * @description: 查詢
     * @return {*}
     */
    queryClick() {
      this.pageNum = 1
      this.getPodsInfo() // 请求数据
    },
    /**
     * @description: 获取容器组
     * @return {*}
     */
    getPodsInfo() {
      // 请求
      getK8sClusterPods({
        ...this.clusterParams,
        keyword: this.searchKey,
        svcName: this.svcName,
        workLoadName: this.workLoadName,
        namespace: this.namespace,
        pageNo: this.pageNum,
        pageSize: this.pageSize
      })
        .then((res) => {
          if (res.data) {
            this.totalNum = res.data.totalNum
            this.list = Array.isArray(res.data.result) ? res.data.result : []
          } else {
            // 数据为空处理
            this.totalNum = 0
            this.list = []
          }
        })
        .finally(() => {
          this.loading = false
        })
    },
    /**
     * @description: 初始化
     * @return {*}
     */
    initTabData() {
      if (this.$route.query.targetTab === 'pods') {
        // 目标Tab是pods时，获取参数
        this.svcName = this.$route.query.svcName || '' // 服务名
        this.workLoadName = this.$route.query.workLoadName || '' // 负载名
        this.namespace = this.$route.query.namespace || '' // namespace
      }
      this.loading = true
      this.pageNum = 1
      this.getPodsInfo() // 获取数据
    }
  }
}
</script>

<style lang="less" scoped>
.pods-mgmt-ctn {
  .pods-table {
    /deep/ .el-table::before {
      background-color: #e9e9e9;
    }
    /deep/ .listHeaderCell {
      height: 40px;
      padding: 0;
      box-sizing: border-box;
      &:first-child {
        padding-left: 20px;
      }
    }
    /deep/ .el-table__body {
      .el-table__row td {
        height: 40px;
        padding: 0;
        &:first-child {
          padding-left: 20px;
        }
      }
    }
  }
  .pods-container-table {
    /deep/ .listHeaderCell {
      &:first-child {
        padding-left: 20px;
      }
    }
    /deep/ .el-table__body {
      .el-table__row td {
        &:first-child {
          padding-left: 20px;
        }
      }
    }
  }
}
</style>
